package com.ks.comment.service;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ks.core.util.JsonUtil;
import com.ks.core.util.ToolUtil;
import com.ks.comment.dao.RedisDao;
import com.ks.comment.entity.UserBase;
import org.springframework.amqp.core.AmqpTemplate;


import javax.annotation.Resource;

/**
 * Created by wanglicheng on 2020/12/9.
 * 可以从配置文件中读取一些配置用于控制Log，MQ，MSG等基础功能
 * 此处如果逻辑相对通用，应考虑把常用逻辑放到croe工程中，以便其它工程的_ServiceBase也可以共用
 * 此处可以用来加载配置文件里的参数，并应用到Croe工程中提供的方法中
 */
public class _ServiceBase<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> {

    @Resource
    private RedisDao redisDao;
    @Resource
    private AmqpTemplate rabbitTemplate;

    /**
     * 判断Token格式，并从Redis中获取用户的相关信息，构造成数据对像供后续逻辑使用
     *
     * @param token
     * @return
     */
    public UserBase LoadUserDataByToken(String token) {
        String userId = getUserId(token);
        if (ToolUtil.isNotEmpty(userId)) {
            String value = redisDao.getValue(userId);
            if (ToolUtil.isNotEmpty(value)) {
                UserBase userBase = JsonUtil.stringToBean(value, UserBase.class);
                if (ToolUtil.isNotEmpty(userBase)) {
                    return userBase;
                }
            }
        }
        return null;
    }

    public String getUserId(String token) {
        if (ToolUtil.isNotEmpty(token)) {
            String[] split = token.split(":");
            String s = split[3];
            if (ToolUtil.isNotEmpty(s)) {
                return s;
            }
        }
        return null;
    }

    public String getTenantId(String token) {
        if (ToolUtil.isNotEmpty(token)) {
            String[] split = token.split(":");
            String s = split[0];
            if (ToolUtil.isNotEmpty(s)) {
                return s;
            }
        }
        return null;
    }

    public String getOrgId(String token) {
        if (ToolUtil.isNotEmpty(token)) {
            String[] split = token.split(":");
            String s = split[1];
            if (ToolUtil.isNotEmpty(s)) {
                return s;
            }
        }
        return null;
    }

    public String getOrgSubId(String token) {
        if (ToolUtil.isNotEmpty(token)) {
            String[] split = token.split(":");
            String s = split[2];
            if (ToolUtil.isNotEmpty(s)) {
                return s;
            }
        }
        return null;
    }


    public void SendMQ(Object obj) {
        rabbitTemplate.convertAndSend("send_share", obj);
        //待设计
    }

    public void WriteLog(String log) {
        //待设计
    }

    public void SendMSG(String msg) {
        //待设计
    }
}
